import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core';
import { CommService } from './../../comm/comm.service';
import { Http, Response } from '@angular/http';
import { Router } from '@angular/router';

import 'rxjs/add/operator/map';
import { Subject } from 'rxjs/Rx';
import { Observable } from 'rxjs/Rx';

import { ReportUrls } from '../../comm/report.urls.conf';
import { SubArea } from '../../model/report/sub-area';
import { QueryModel } from '../../model/report/query-model';
import { ResponseModel } from '../../model/comm/response-model';

declare const $: any;
import * as moment from 'moment';
import { TVResource } from 'app/ptpmc/model/comm/tv-resource';
import { DxDataGridComponent } from '../../../../../node_modules/devextreme-angular';
import { ScheduleHistory } from 'app/ptpmc/model/report/schedule_history';
declare var swal: any;

@Component({
    selector: 'app-config-sh',
    templateUrl: './sh.component.html',
    styleUrls: ['./sh.component.css'],
})

export class SHComponent implements OnInit, AfterViewInit {

    @ViewChild(DxDataGridComponent) dataGrid: DxDataGridComponent;
    /**查询按类型 */
    public typeItems = [{ id: 1, text: '按值统计' }, { id: 2, text: '按班次统计' }];
    public typeSelected = this.typeItems[0];

    /**选择项 */
    public items: ScheduleHistory[] = [];
    public selectedItems: ScheduleHistory[] = [];


    /*生产线 - 班次*/
    public subAreaItems: SubArea[];

    public shiftItems: any[];
    public subAareSelected: any;

    filterVlaueItems: string[] = ['All'];
    addFilterVlaueItemItem(data) {
        var newItem = data.text;
        this.filterVlaueItems.push(newItem)
        return newItem;
    }

    filterChange(data) {
        if (data.value == "All") {
            this.dataGrid.instance.clearFilter();
        } else {
            this.dataGrid.instance.filter(["duration", ">", data.value]);
        }
    }

    constructor(private commServices: CommService) {
    }
    ngOnInit() {
        this.commServices.initDatePicker($('#DateTimeRange'), 'YYYY-MM-DD HH:mm');
        this.shiftItems = this.commServices.shifts;
        this.subAreaItems = this.commServices.getSubAreas();
        this.subAareSelected = this.subAreaItems[0];
    }

    ngAfterViewInit() {
    }

    /**获取表格数据*/
    getReportData(): void {
        debugger;
        const queryModel: QueryModel = new QueryModel();
        queryModel.dateTimeRange = this.getTime('DateTimeRange');
        queryModel.querySubAreaIds = [this.subAareSelected.subArea];
        const url: string = ReportUrls.SHFilterUrl + "/1";
        this.commServices
            .getList(queryModel, url)
            .subscribe(model => {
                this.items = [];
                if (model.resultCode === 200) {
                    this.items = model.data;
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
    }
    public updateFilterList: ScheduleHistory[] = [];
    onCheckBoxChange(entity: ScheduleHistory): void {
        let index: number = this.items.findIndex(e => e.subArea === entity.subArea && e.productionDate === entity.productionDate
            && e.shift === entity.shift);
        this.items
        if (index != -1) {
            this.items[index].reportFilter = entity.reportFilter === 1 ? 0 : 1;
            let upIndex: number = this.updateFilterList.findIndex(e => e.subArea === entity.subArea && e.productionDate === entity.productionDate
                && e.shift === entity.shift);
            if (upIndex != -1) {
                this.updateFilterList[upIndex] = this.items[index];
            }
            else {
                this.updateFilterList.push(this.items[index]);
            }
        }
    }
    saveChanges() {
        this.commServices.update(this.updateFilterList, ReportUrls.SHFilterUpdateUrl + "/1")
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.updateFilterList = [];
                    alert("更新成功！");
                } else {
                    // console.log(model.resultCode + ',' + model.resultMessage);
                    alert('更新失败！' + model.resultMessage);
                }
            });
    }
    /**获取TimeRange的值*/
    getTime(id: any): String {
        const timerange: String = $('#' + id).val();
        return timerange;
    }

}
